package com.hjx.windowtool.file;

import java.awt.EventQueue;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.hjx.windowtool.db.Tool;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.List;
import java.awt.event.ActionEvent;

@Tool("文件分割")
public class FileSplitApp {

	private JFrame frame;
	private JTextField txt_file;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					FileSplitApp window = new FileSplitApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileSplitApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("文件分割");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.5);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("文件");
		label.setBounds(10, 10, 54, 15);
		panel.add(label);
		
		txt_file = new JTextField();
		txt_file.setBounds(74, 7, 326, 21);
		panel.add(txt_file);
		txt_file.setColumns(10);
		new DropTarget(txt_file, new DropTargetAdapter() {
			
			@Override
			public void drop(DropTargetDropEvent e) {
				try {
					System.out.println(e);
					Transferable transferable = e.getTransferable();
					if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
						e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
						List<?> list = (List<?>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
						System.out.println(list);
						StringBuffer sb=new StringBuffer();
						for (Object object : list) {
							sb.append(","+object);
						}
						if (sb.length()>0) {
							sb.deleteCharAt(0);
						}
						txt_file.setText(sb.toString());
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		JLabel label_1 = new JLabel("分割大小");
		label_1.setBounds(10, 35, 54, 15);
		panel.add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setText("10");
		textField_1.setBounds(74, 32, 48, 21);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblM = new JLabel("M");
		lblM.setBounds(132, 35, 54, 15);
		panel.add(lblM);
		
		JButton button = new JButton("分割");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String file = txt_file.getText();
				splitFile(file,Integer.valueOf(textField_1.getText()));
			}
		});
		button.setBounds(74, 66, 63, 23);
		panel.add(button);
		
		JButton button_1 = new JButton("合并");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				combineFile(Arrays.asList(txt_file.getText().split(",")));
			}
		});
		button_1.setBounds(142, 66, 93, 23);
		panel.add(button_1);
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
	}

	protected void combineFile(List<String> list) {
		try {
			File f = new File(list.get(0));
			File saveFile=new File(f.getParent(),"combine_"+f.getName());
			if (!saveFile.exists()) {
				saveFile.createNewFile();
			}
			RandomAccessFile sraf=new RandomAccessFile(saveFile, "rw");
			byte[] b = new byte[1024];
			for (int i = 0; i < list.size(); i++) {
				File file = new File(list.get(i));
				RandomAccessFile raf=new RandomAccessFile(file, "r");
				int read=-1;
				while ((read = raf.read(b))!=-1) {
					sraf.write(b, 0, read);
				}
				raf.close();
			}
			sraf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	protected void splitFile(String file, int size) {
		try {
			File f = new File(file);
			if (!f.exists()) {
				return;
			}
			long splitSize=size*1024*1024;
			long length = f.length();
			RandomAccessFile raf=new RandomAccessFile(f, "r");
			byte[] b = new byte[1024];
			for (int i = 0; i < length/splitSize+(length%size>0?1:0); i++) {
				File file2 = new File(f.getParent(),i+"_"+f.getName());
				if (!file2.exists()) {
					file2.createNewFile();
				}
				System.out.println(file2);
				RandomAccessFile fos=new RandomAccessFile(file2,"rw");
				for (long j = i*splitSize; j < Math.min(i*splitSize+splitSize, length);) {
					int read = raf.read(b);
					fos.write(b, 0, read);
					j+=read;
				}
				fos.close();
			}
			raf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
